package myface;

import java.io.File;
import java.util.HashMap;

import javax.sound.sampled.SourceDataLine;

import com.github.kevinsawicki.http.HttpRequest;
import com.google.gson.Gson;
/**
 * 人脸引擎的api代理，业务层只需要调用这个文件的方法
 */
public class FaceProxy {
    // 默认使用本机
   public static String host="http://localhost:18001";
//    public static String wsHost="ws://face.doswalker.com:11234";
   public static String wsHost="ws://localhost:11234";

   /**
    * 上传文件
    * @param path : 文件路径
    * @return : 远程路径
    */
   public static  String uploadImage(String path){
      HttpRequest request = HttpRequest.post(host+"/api/upload");
      if(path!=null && !path.isEmpty()){
        File file =new File(path);
        if(file.exists()){
           request.part("face_img", file.getName(), file);
        }
      }
      String response = request.body();
      System.out.println(response);
      if (response.indexOf("result:") == 0) {
          return response.split(":")[1];
      }
      throw new RuntimeException(response);
   }
   /**
    * 
    * @param name 注册到人脸引擎的名称,可以使用[/]分割，如：/财务部/张三/13092888227
    * @param filePath 文件路径，可以是网络地址，或者和人脸引擎同一台机器的图片全路径(host不是本机地址时要注意)
    * @return
    */
   public static boolean registerPerson(String name, String filePath) {
       if(host.indexOf("localhost")<0){//如果是远程调试，上传文件；否则使用本地路径
           filePath = uploadImage(filePath);
       }
       Gson gson = new Gson();
       HashMap data = new HashMap<>();
       HashMap payload = new HashMap<>();
       payload.put("name", name);
       payload.put("path", filePath);
    //    payload.put("path", "/home/czj/图片/2020-09-23-023219.jpg");
       data.put("payload", payload);
       String dataStr = gson.toJson(data);
    //    System.out.println(dataStr);
       String response = HttpRequest.post(host+"/api/registerPerson").send(dataStr).body();
       System.out.println("Response was: " + response);
       if(response.indexOf("result:")==0){
           return true;
       }
       throw new RuntimeException(response);
   }

   public static HashMap getPagedFaces(int pageNO, int pageSize){
       //TODO: 分页查询
       String response = HttpRequest.get(host + "/api/getPagedFaces").body();
       Gson gson = new Gson();
       HashMap result = gson.fromJson(response, HashMap.class);
       System.out.println(result.get("result"));
     return result;
   }

   /**
    * 删除人脸信息
    * @param name  registerPerson 方法传入的name
    * @return
    */
   public static boolean removePerson(String name){
       Gson gson = new Gson();
       HashMap data = new HashMap<>();
       HashMap payload = new HashMap<>();
       payload.put("path", name);
       data.put("payload", payload);
       String dataStr = gson.toJson(data);
    //    System.out.println(dataStr);
       String response = HttpRequest.post(host+"/api/removePerson").send(dataStr).body();
       System.out.println("Response was: " + response);
       if(response.indexOf("result:")==0){
           return true;
       }
       throw new RuntimeException(response);
   }

   /**
    * 启用1:n 匹配
    * @return
    */
   public static boolean startOne2Many(){
       String response = HttpRequest.post(host+"/api/start_one2many").send("").body();
       System.out.println("Response was: " + response);
       if(response.indexOf("result:")==0){
           return true;
       }
       throw new RuntimeException(response);
   }
   /**
    * 1:1 匹配 , 匹配摄像头捕捉的照片和参数传递的照片，是不是同一个人, 结果通过EmptyClient的onMessage通知
    * @param path : 1:1 需要比对的图片
    * @return
    */
   public static boolean startOne2One(String path){
      HttpRequest request = HttpRequest.post(host+"/api/start_one2one");
      if(path!=null && !path.isEmpty()){
        File file =new File(path);
        if(file.exists()){
           request.part("face_img", file.getName(), file);
        }
      }
      String response = request.body();
      if (response.indexOf("result:") == 0) {
          return true;
      }
      throw new RuntimeException(response);
   }

   public static String getConfig(){
       String response = HttpRequest.get(host+"/api/getConfig").body();
       System.out.println("Response was: " + response);
       return response;
   }
        // svr.Get("/api/setConfig", [&](const Request& req, Response& res)->void {


   public static boolean setConfig(float score) {
       Gson gson = new Gson();
       HashMap data = new HashMap<>();
       HashMap payload = new HashMap<>();
       data.put("recognize_threshold", score);
       data.put("recognize_in_server", false);
       String dataStr = gson.toJson(data);
       String response = HttpRequest.post(host+"/api/setConfig").send(dataStr).body();
       System.out.println("Response was: " + response);
       if(response.indexOf("result:")==0){
           return true;
       }
       throw new RuntimeException(response);
   }
}
